package fun.werfamily.wf.user.core.domain.util;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/**
 * Random verification code generation rules , For page anti brute force cracking
 * @author YINGFU
 */
public class CaptchaUtil {
    public static final String SESSION_KEY = "CAPTCHA"; // session The property name
    private static final int WIDTH = 80; // Image width
    private static final int HEIGHT = 26; // Picture height
    private static final int LINESIZE = 8; // The number of interference lines in the picture
    private static final int NUMBER = 4; // The number of characters produced
    private static final int FONTSIZE = 18; // Picture text size
    private static final int R = 255;
    private static final int G = 255;
    private static final int B = 255;
    private static final int DEFAULT_FONT_STYLE = Font.CENTER_BASELINE; // Default style
    private static final String DEFAULT_FONT_FAMILY = Font.SANS_SERIF; // default font
    /**
     * Random number generation pool
     */
    private static final char[] CHARSPOOL = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
            'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z' };
    private static Random random = new Random();
    /**
     * Generate random characters
     * @return string
     */
    private static char randomCode(){
        int length = CHARSPOOL.length;
        int num = random.nextInt(length);
        return CHARSPOOL[num];
    }
    /**
     * Style random colors
     * @return color
     */
    private static Color colorStyle(){
        int r = random.nextInt(R);
        int g = random.nextInt(G);
        int b = random.nextInt(B);
        return new Color(r, g, b);
    }
    /**
     * Set the basic style of the font
     * @return font
     */
    private static Font fontStyle(){
// by linux Server usage
        System.setProperty("java.awt.headless", "true");
        return new Font(DEFAULT_FONT_FAMILY, DEFAULT_FONT_STYLE, FONTSIZE);
    }
    /**
     * Set the color of the text
     * @return
     */
    private static Color fontColorStyle(){
        int r = random.nextInt(100);
        int g = random.nextInt(100);
        int b = random.nextInt(100);
        return new Color(r, g, b);
    }
    /**
     * Drawing random numbers
     * @param g
     * @param i
     * @return char
     */
    private static char drawRandomCode(Graphics g, int i){
        g.setFont(fontStyle());
// g.setColor(colorStyle());
        g.setColor(fontColorStyle());
        char randomCode = randomCode();
        g.translate(random.nextInt(3) , random.nextInt(3));
        g.drawString(String.valueOf(randomCode), 13 * i, 16);
        return randomCode;
    }
    /**
     * Draw interference line
     * @param g
     */
    private static void drawLine(Graphics g){
        int x = random.nextInt(WIDTH);
        int y = random.nextInt(HEIGHT);
        int x2 = random.nextInt(WIDTH/2);
        int y2 = random.nextInt(HEIGHT/2);
        g.drawLine(x, y, x2, y2);
    }
    /**
     * Generate verification code
     * @param session
     * @param response
     */
    public static String buildCaptcha(HttpSession session, HttpServletResponse response){
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setFont(fontStyle());
        g.setColor(colorStyle());
        for(int i=0;i<LINESIZE;i++){
            drawLine(g);
        }
        StringBuilder code = new StringBuilder();
        for(int i=1; i<= NUMBER; i++){
            code.append(drawRandomCode(g, i));
        }
//        session.removeAttribute(SESSION_KEY);
//        session.setAttribute(SESSION_KEY, code.toString());
        g.dispose();
        try {
            ImageIO.write(image, "JPEG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code.toString();
    }
}
